<?php
/**
 * [模型]管理员
 * 
 * @version $Id: Admin.php 229 2012-01-19 06:40:25Z linsir123 $
 */
class adminAdmin
{
	/**
	 * @var 密码散列扰码
	 */
	public $pwdHash = array('linxs@','||e35go~!@#');
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		@session_start();
	}
	
	/**
	 * 登录
	 */
	public function login($userName, $password)
	{
		$flag = false;
		$db = D::admin();
		$admin = $db->getBy('username', $userName);
		if ( ! empty($admin)) {
			$pwdHash = md5($this->pwdHash[0].$password.$this->pwdHash[1]);
			$flag = strcmp($pwdHash, $admin['password'])===0;
			if ($flag) {
				$db->updateById($admin['id'], array(
					'last_time' => date('Y-m-d H:i:s'),
					'last_ip' => Request::ip()
				));
				
				$_SESSION['admin'] = array('username' => $admin['username']);
			}
		}
		
		return $flag;
	}
	
	/**
	 * 登录状态
	 */
	public function isLogin()
	{
		return isset($_SESSION['admin']) && ! empty($_SESSION['admin']);
	}
	
	/**
	 * 登出
	 */
	public function logout()
	{
		@session_destroy();
		return true;
	}
}
